package wt.hpad.model.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import wt.hpad.MainApp;
import wt.hpad.R;
import wt.hpad.model.bean.TableInfo;
import wt.hpad.utils.AppUtil;

/**
 * 桌台数据适配器
 * Created by wt on 12.22.
 */

public class TableAdapter extends BaseAdapter {
    private List<TableInfo> mTbArr;
    private int width, height, colNum;
    private Context mContext;

    public TableAdapter(Context context, List<TableInfo> tbArr, int width, int height, int colNum) {
        mTbArr = tbArr;
        mContext = context;
        this.width = width;
        this.height = height;
        this.colNum = colNum;
    }

    @Override
    public int getCount() {
        return mTbArr.size();
    }

    @Override
    public Object getItem(int i) {
        return mTbArr.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(MainApp.getContext()).inflate(R.layout.item_table, viewGroup, false);
        }
        ImageView iv_table = (ImageView) view.findViewById(R.id.iv_icon_table);
        if (mTbArr.get(position).status == 1) {
            iv_table.setBackground(AppUtil.getDrawable(mContext, R.drawable.table_use));
        } else if (mTbArr.get(position).status == 3) {
            iv_table.setBackground(AppUtil.getDrawable(mContext, R.drawable.table_free));
        }
        TextView tv_tableID = (TextView) view.findViewById(R.id.tv_tableID);

        ViewGroup.LayoutParams layoutParams = iv_table.getLayoutParams();
        layoutParams.height = width / colNum;
        layoutParams.width = width / colNum;
        iv_table.setLayoutParams(layoutParams);
        tv_tableID.setText(mTbArr.get(position).tableID);

        return view;
    }

    public void refreshItems(List<TableInfo> datas) {
        mTbArr.clear();
        mTbArr.addAll(datas);
        this.notifyDataSetChanged();
    }

}
